package praktikumid.w09.p5.interface_example;

import java.util.ArrayList;
import java.util.List;

/**
 * Some examples about class usage.
 * @author Ago
 *
 */
public class Main {

	/**
	 * The main.
	 * @param args arguments
	 */
	public static void main(String[] args) {
		// we cannot instantiate abstract class
		//Animal a = new Animal();
		Cat c = new Cat();
		
		Animal a = new Cat();
		a = new Goldfish();

		// let's have a list of animals
		// Every list element has to be
		// Animal or its subclass
		List<Animal> animals = new ArrayList<Animal>();
		// let's fill 
		animals.add(c);
		animals.add(a);
		animals.add(new Frog());
		
		// let's loop through the list
		for (Animal an : animals) {
			if (an instanceof Goldfish) {
				// to check whther the given
				// animal is Goldfish 
				System.out.println("goldfish!");
			}
			if (an instanceof Animal) {
				// as you can see, every
				// animal is an instance of Animal
				System.out.println("animal");
			}

			if (an instanceof IMovesOnLand) {
				// you can check whether
				// an object is implementation of
				// an interface
				System.out.println("walks");
				// in order to access walk method
				// we have to say that
				// the given animal is of type IMoveOnLand.
				// We use casting for that: 
				// (X) o
				// casts object o into type of X.
				// In the memory, nothing changes for
				// the object.
				IMovesOnLand l = (IMovesOnLand) an;
				l.walk();
				// oneliner to access the method
				((IMovesOnLand) an).walk();
			}
		}
	}

}
